# -*- coding = utf-8 -*-
# @Time : 2023/4/3 14:10
# @Author : 龙王赘婿_彪少
# @File : juying_IO.py
# @Software: PyCharm

# 功能：聚英电子IO控制

import modbus_tk.defines as cst
from modbus_tk import modbus_tcp


class Modbus:
    def __init__(self):
        self.client = modbus_tcp.TcpMaster('192.168.88.232', 10000)
        self.client.set_timeout(1)

    def control(self, io, value):
        try:
            res = self.client.execute(1, cst.WRITE_SINGLE_COIL, starting_address=io, output_value=value)
            print(res)  # 65280 FF00
        except Exception as err:
            print("异常：", err)


if __name__ == "__main__":
    m = Modbus()
    while True:
        key = input("输入指令：")
        if key == "1o":
            m.control(0, 1)
        elif key == "1c":
            m.control(0, 0)
        elif key == "2o":
            m.control(1, 1)
        elif key == "2c":
            m.control(1, 0)
        elif key == "3o":
            m.control(2, 1)
        elif key == "3c":
            m.control(2, 0)
        elif key == "4o":
            m.control(3, 1)
        elif key == "4c":
            m.control(3, 0)

